
package com.cjj.elasticsearch.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;


/**
 * @ClassName ElasticsearchConfig
 * @Date 2021/11/03 16:30:40
 * @Created by BrownC_
 * @email ccc-ju@outlook.com
 */


@Configuration
@ConfigurationProperties(prefix = "elastic")
@EnableElasticsearchRepositories
public class ElasticsearchConfig {

    /**
     * elasticsearch连接node, 127.0.0.1:9200
     */

    private String host;

    /**
     * elasticserach连接认证用户名
     */

    private String username;

    /**
     * elasticserach连接认证密码
     */

    private String password;

    @Bean
    RestHighLevelClient elasticsearchClient() {
        ClientConfiguration configuration = ClientConfiguration.builder()
                .connectedTo(host)
                //.withConnectTimeout(Duration.ofSeconds(5))
                //.withSocketTimeout(Duration.ofSeconds(3))
                //.useSsl()
                //.withDefaultHeaders(defaultHeaders)
                .withBasicAuth(username, password)
                // ... other options
                .build();
        RestHighLevelClient client = RestClients.create(configuration).rest();
        return client;
    }

}
